// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using NUnit.Framework;
using System;
using WinPcap;
using WinPcap.Tests.Utils;

namespace WinPcap.Tests
{
	/// <summary>
	/// Tests the IPPacket class.
	/// </summary>
	[ TestFixture ]
	public class IPPacketTestFixture
	{
		/// <summary>
		/// Tests a Tcp SYN packet.
		/// </summary>
		[ Test ]
		public void TcpSynPacket( )
		{
			string Data = 
				"0000  45 00 00 30 45 C0 40 00  80 06 23 02 C8 01 01 01 \n" +
				"0010  C8 01 01 02 04 4A 1B 5A  D0 22 05 0A 00 00 00 00 \n" +
				"0020  70 02 FF FF FC 48 00 00  02 04 05 B4 01 01 04 02 ";

			byte[ ] Bytes = TestUtility.GetBytesFromString( Data ); 

			IPPacket Packet = new IPPacket( Bytes );

			// Check the byte count.

			Assert.AreEqual( Bytes.Length,
				Packet.GetBytes( ).Length,
				"IP Packet byte count is incorrect." );

			// IP version.

			Assert.AreEqual( 4, Packet.Version, "IP version mismatch." );

			// IP header length in bytes.

			Assert.AreEqual( 20, Packet.HeaderLength, 
				"IP header length is incorrect." );

			// Protocol.

			Assert.AreEqual( Protocol.Tcp, Packet.Protocol,
				"Expected TCP protocol." );

			// Source IP address.

			Assert.AreEqual( "200.1.1.1", Packet.Source.ToString( ),
				"Source IP address mismatch." );

			// Destination IP address.

			Assert.AreEqual( "200.1.1.2", Packet.Destination.ToString( ),
				"Source IP address mismatch." );

			// Time to live.

			Assert.AreEqual( 128, Packet.TimeToLive, "Time to live mismatch." );

			// Inner packet.

			Assert.IsNotNull( Packet.InnerPacket, "Inner packet should not be null." );
			Assert.AreEqual( typeof( TcpPacket ), 
				Packet.InnerPacket.GetType( ),
				"Expecting Tcp Packet as the inner packet." );
		}

		/// <summary>
		/// Tests that a fragmented packet is handled correctly.  The first
		/// fragment will contain the header of the packet inside this one 
		/// (e.g. TCP), but other fragments of the packet will not.  If 
		/// we have the first fragment we can attempt to read the inner packet's 
		/// header otherwise we should not.
		/// </summary>
		public void TestFragmentedPacket( )
		{
		}

		/// <summary>
		/// Tests an attempt to decode a packet with an IP header that
		/// is truncated.
		/// </summary>
		[ Test ]
		public void TruncatedIPPacketHeader( )
		{
			// Data truncated so it is smaller than the minimum header size.

			string Data = 
				"0000  45 00 00 30 45 C0 40 00  80 06 23 02 C8 01 01 01 \n" +
				"0010  C8 01 01";

			byte[ ] Bytes = TestUtility.GetBytesFromString( Data ); 

			IPPacket Packet = new IPPacket( Bytes );

			// Check the byte count.

			Assert.AreEqual( Bytes.Length,
				Packet.GetBytes( ).Length,
				"IP Packet byte count is incorrect." );

			// IP header length in bytes.

			Assert.AreEqual( 20, Packet.HeaderLength, 
				"IP header length is incorrect." );

			// Check destination is null.
			Assert.AreEqual( null, Packet.Destination, 
				"Should not have extracted destination" );

			// Check inner packet.
			Assert.AreEqual( null, 
				Packet.InnerPacket, 
				"Should not have an inner data packet." );

		}

		/// <summary>
		/// Tests an attempt to decode a packet with no inner packet.
		/// </summary>
		[ Test ]
		public void PacketWithNoPayloadData( )
		{
			// Data only contains the IP header.

			string Data = 
				"0000  45 00 00 30 45 C0 40 00  80 06 23 02 C8 01 01 01 \n" +
				"0010  C8 01 01 02";

			byte[ ] Bytes = TestUtility.GetBytesFromString( Data ); 

			IPPacket Packet = new IPPacket( Bytes );

			// Check the byte count.

			Assert.AreEqual( Bytes.Length,
				Packet.GetBytes( ).Length,
				"IP Packet byte count is incorrect." );

			// IP header length in bytes.

			Assert.AreEqual( 20, Packet.HeaderLength, 
				"IP header length is incorrect." );

			// Protocol.

			Assert.AreEqual( Protocol.Tcp, Packet.Protocol,
				"Expected TCP protocol." );

			// Source IP address.

			Assert.AreEqual( "200.1.1.1", Packet.Source.ToString( ),
				"Source IP address mismatch." );

			// Destination IP address.

			Assert.AreEqual( "200.1.1.2", Packet.Destination.ToString( ),
				"Source IP address mismatch." );

			// Time to live.

			Assert.AreEqual( 128, Packet.TimeToLive, "Time to live mismatch." );

			// Inner packet.

			Assert.IsNull( Packet.InnerPacket, "Inner packet should be null." );
		}
	}
}

